// ComponentOne.vue
<template>
  <div>
    <h1>用户登录页面</h1>
    <form @submit.prevent="submitForm">
      <label for="username">账号:</label><br>
      <input v-model="username" type="text" id="username" name="username" required><br>
      <label for="password">密码:</label><br>
      <input v-model="password" type="password" id="password" name="password" required><br>
      <input type="submit" value="登录">
    </form>
    <br>
    <button @click="switchLogin">切换到管理员登录页面</button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'ComponentOne',
  data() {
    return {
      username: '',
      password: ''
    };
  },
  methods: {
    submitForm() {
      axios.post('http://localhost:8889/api/user/login', {
        username: this.username,
        password: this.password
      })
      .then(response => {
        if (response.status === 200) {

          localStorage.setItem('username', this.username);

          this.$router.push({ name: 'ComponentTwo' });
        } else {
          alert('账号密码错误，登录失败');
        }
      })
      .catch(error => {
        console.error(error);
      });
    },
    switchLogin(){
      this.$router.push('/manager-login')
    }
  }
}
</script>
